// tabledeleterow.js version 1.2 2006-02-21
// mredkj.com

// CONFIG notes. Below are some comments that point to where this script can be customized.
// Note: Make sure to include a <tbody></tbody> in your table's HTML

var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE_NAME = 'tblSample'; // this should be named in the HTML
var ROW_BASE = 1; // first number (for display)
var hasLoaded = false;

window.onload=fillInRows2;

function fillInRows(numrows)
{
	var i;
	hasLoaded = true;
	//alert ('Vao ham');
	if (document.getElementById && document.getElementsByTagName) {
		//alert ('Vao if');
		var tbl = document.getElementById(TABLE_NAME);
		var l = tbl.rows.length;
		//alert (l);
		if(l > 1){
			delallrow(tbl, l);
		}
		if(numrows){
			/*for(i=0;i<numrows;i++)
				addRowToTable(null);
			}*/
			//alert (numrows);
			i = 0;
			while(i<numrows && i<21){
				addRowToTable2(null,'','','','');
				//alert (i);
				i++;
				//alert (numrows);
			}
			if(i>=21){
				document.forms['maphat'].TERMNO.value = '20';
			}	
		}
	}
	//addRowToTable();
}
function fillInRows2(){
	hasLoaded = true;
}
// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four, five, six, seven, eight)
{
	//alert('123');
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input checkbox object
	this.four = four; // input radio object
	this.five = five;
	this.six = six;
	this.seven = seven;
	this.eight = eight;
}

/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
 /*
function addRowToTable(num)
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME);
		var nextRow = tbl.tBodies[0].rows.length;
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		// add the row
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
		//row.className = 'classy' + (iteration % 2);
	
		// CONFIG: This whole section can be configured
		
		// cell 0 - text
		var cell0 = row.insertCell(0);
		cell0.setAttribute('align','center');
		cell0.style.padding = '.1em 0em';
		var textNode = document.createTextNode(iteration);
		
		cell0.appendChild(textNode);
		
		// cell 1 - input text
		var cell1 = row.insertCell(1);
		cell1.style.padding = '.1em 2px';
		//cell1.setAttribute('align','center');
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', 'TERM' + iteration);
		txtInp1.setAttribute('size', '15');
		//txtInp1.setAttribute('value', 'TERM' + iteration); // iteration included for debug purposes
		cell1.appendChild(txtInp1);
		
		var cell2 = row.insertCell(2);
		cell2.style.padding = '.1em 2px';
		//cell2.setAttribute('align','center');
		var sel = document.createElement('select');
		sel.name = 'TRMCD' + iteration;
		//sel.style = 'width:150px';
		sel.setAttribute('style', 'width:150px;');
		sel.options[0] = new Option('D - Ngay', 'D');
		sel.options[1] = new Option('M - Thang', 'M');
		sel.options[2] = new Option('Y - Nam', 'Y');
		cell2.appendChild(sel);
		
		var cell3 = row.insertCell(3);
		cell3.style.padding = '.1em 2px';
		//cell3.setAttribute('align','center');
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'text');
		txtInp3.setAttribute('name', 'RATE' + iteration);
		txtInp3.setAttribute('size', '15');
		txtInp3.className = 'textright';
		txtInp3.onblur = function () {checkDecimal(this,this.value)};
		//txtInp3.setAttribute('value', 'RATE' + iteration); // iteration included for debug purposes
		cell3.appendChild(txtInp3);
		
		var cell4 = row.insertCell(4);
		cell4.style.padding = '.1em 2px';
		//cell4.setAttribute('align','center');
		var txtInp4 = document.createElement('input');
		txtInp4.setAttribute('type', 'text');
		txtInp4.setAttribute('name', 'RATECD' + iteration);
		txtInp4.setAttribute('size', '15');
		txtInp4.setAttribute('id', 'RATECD' + iteration);
		//txtInp4.setAttribute('value', 'RATECD' + iteration); // iteration included for debug purposes
		cell4.appendChild(txtInp4);
		
		//cell 2 - input button
		var cell5 = row.insertCell(5);
		cell5.style.padding = '.1em 2px';
		var btnEl = document.createElement('input');
		btnEl.setAttribute('type', 'button');
		btnEl.setAttribute('value', '    ');
		btnEl.onclick = function () {openwindow(this,iteration)};
		btnEl.setAttribute('id','replacement_binoculars');
		cell5.appendChild(btnEl);
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(textNode, txtInp1, sel, txtInp3, txtInp4, btnEl);
	}
}
*/
//them moi
function addRowToTable2(num,TERM,TRMCD,RATE,RATECD)
{
	//alert(hasLoaded);
	//alert('123');
	if (hasLoaded && document.getElementById && document.getElementsByTagName) {
		var textbox = document.getElementById('TERMNO');
		var btn_addrecord = document.getElementById('addrecord');
		if(textbox.value>=21){
			btn_addrecord.disabled = true;
			return;	
		}
	 	//alert('ho tro dom');	
		var tbl = document.getElementById(TABLE_NAME);
	
		var nextRow = tbl.tBodies[0].rows.length;
		
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		// add the row
		
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
		//row.className = 'classy' + (iteration % 2);
	
		// CONFIG: This whole section can be configured
		
		// cell 0 - text
		var cell0 = row.insertCell(0);
		cell0.setAttribute('align','center');
		cell0.style.padding = '.0em 0em';
		var textNode = document.createTextNode(iteration);
		cell0.appendChild(textNode);
		
		// cell 1 - input text
		var cell1 = row.insertCell(1);
		cell1.style.padding = '.0em 0px';
		//cell1.setAttribute('align','center');
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', 'TERM' + iteration);
		txtInp1.setAttribute('size', '20');
		txtInp1.setAttribute('maxlength', '3');
		txtInp1.setAttribute('value', TERM); // iteration included for debug purposes
		cell1.appendChild(txtInp1);
		
		var cell2 = row.insertCell(2);
		cell2.style.padding = '.0em 0px';
		//cell2.setAttribute('align','center');
		var sel = document.createElement('select');
		sel.name = 'TRMCD' + iteration;
		//sel.style = 'width:150px';
		//sel.setAttribute('style', 'width:150px;');
		sel.options[0] = new Option('D - Ngay', 'D');
		sel.options[1] = new Option('M - Thang', 'M');
		sel.options[2] = new Option('Y - Nam', 'Y');
		if(TRMCD=='D'){
			sel.options[0].selected = true;
		}else{
			if(TRMCD=='M'){
				sel.options[1].selected = true;
			}
			else{
				if(TRMCD=='Y'){
					sel.options[2].selected = true;
				}
			}
		}
		cell2.appendChild(sel);
		
		var cell3 = row.insertCell(3);
		cell3.style.padding = '.0em 0px';
		//cell3.setAttribute('align','center');
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'text');
		txtInp3.setAttribute('name', 'RATE' + iteration);
		txtInp3.setAttribute('size', '20');
		txtInp3.setAttribute('maxlength', '7');
		txtInp3.setAttribute('value', RATE); // iteration included for debug purposes
		txtInp3.onblur = function () {formatNumber2(this,this.value,4,4);checkDecimal(this,this.value)};
		
		txtInp3.className = 'textright';
		cell3.appendChild(txtInp3);
		
		var cell4 = row.insertCell(4);
		cell4.style.padding = '.0em 0px';
		//cell4.setAttribute('align','center');
		var txtInp4 = document.createElement('input');
		txtInp4.setAttribute('type', 'text');
		txtInp4.setAttribute('name', 'RATECD' + iteration);
		txtInp4.setAttribute('size', '20');
		txtInp4.setAttribute('id', 'RATECD' + iteration);
		txtInp4.setAttribute('value', RATECD); // iteration included for debug purposes
		txtInp4.setAttribute('maxlength', '5');
		//txtInp4.className = 'textcenter';
		cell4.appendChild(txtInp4);
		
		//cell 2 - input button
		var cell5 = row.insertCell(5);
		cell5.style.padding = '0px 5px';
		var btnEl = document.createElement('input');
		btnEl.setAttribute('type', 'button');
		btnEl.setAttribute('value', '    ');
		btnEl.onclick = function () {openwindow(this,iteration)};
		btnEl.setAttribute('id','replacement_binoculars');
		cell5.appendChild(btnEl);
		
		// cell 2 - input button
		var cell6 = row.insertCell(6);
		cell6.style.padding = '.0em 5px';
		var btnE2 = document.createElement('input');
		btnE2.setAttribute('type', 'button');
		btnE2.setAttribute('value', '     ');
		btnE2.setAttribute('id','replacement_delete');
		btnE2.onclick = function () {deleteCurrentRow(this)};
		cell6.appendChild(btnE2);
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(textNode, txtInp1, sel, txtInp3, txtInp4, btnEl, btnE2);
		
		textbox.value = tbl.tBodies[0].rows.length;
	}
}

// CONFIG: this entire function is affected by myRowObject settings
// If there isn't a checkbox in your row, then this function can't be used.
/*function deleteChecked()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
	
		var tbl = document.getElementById(TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.three.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.three.checked) {
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
			}
		}
		if (checkedObjArray.length > 0) {
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			//reorderRows(tbl, rIndex);
		}
	}
}*/

// If there isn't an element with an onclick event in your row, then this function can't be used.
/*function deleteCurrentRow(obj)
{
	var tbl = document.getElementById(TABLE_NAME);
	
	if (hasLoaded) {
		
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		reorderRows(tbl, rIndex);
	}

}*/
function deleteCurrentRow(obj)
{
//	alert('vao ham');
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		reorderRows(tbl, rIndex);
		var btn_addrecord = document.getElementById('addrecord');
		if(btn_addrecord){
			btn_addrecord.disabled = false;	
		}
	}

}
/*
function orderRow(tbl,startingIndex){
	if (hasLoaded) {
		if (tbl.tBodies[0].rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
					
				tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}
}
*/
function reorderRows(tbl, startingIndex)
{
	if (hasLoaded) {
		//document.maphat.TERMNO.value =  tbl.tBodies[0].rows.length;
		/*var textbox = document.maphat.TERMNO;
		if(textbox){
			alert('Co');	
		}else{
			alert('Khong');
		}*/
		var textbox = document.getElementById('TERMNO');
		textbox.value = tbl.tBodies[0].rows.length;
		//alert(tbl.tBodies[0].rows.length);
		if (tbl.rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
			
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.two.name = 'TERM' + count; // input text
				tbl.tBodies[0].rows[i].myRow.three.name = 'TRMCD' + count; // input text
				tbl.tBodies[0].rows[i].myRow.four.name = 'RATE' + count; // input text
				tbl.tBodies[0].rows[i].myRow.five.name = 'RATECD' + count; // input text
				
				//tbl.tBodies[0].rows[i].myRow.two.value = 'TERM' + count; // input text
				// CONFIG: next line is affected by myRowObject settings
				//var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
				//tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
				
				// CONFIG: next line is affected by myRowObject settings
				//tbl.tBodies[0].rows[i].myRow.four.value = count; // input radio
				
				// CONFIG: requires class named classy0 and classy1
				//tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}

}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
	
}

function openInNewWindow(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	// set the target to the blank window
	frm.target = 'TableAddRow2NewWindow';
	
	// submit
	frm.submit();
}

function delallrow(moduleTable,l) {
len = l;
	for (i = len -1; i != 0; i--) {
	
		moduleTable.deleteRow(i);
	
	}
}

function openwindow(form, index){
	mywindow = window.open('lai_suat_id.php?name=RATECD' + index, 'mywindow','menubar=0,resizable=yes,width=800,height=300, scrollbars=yes');
	mywindow.moveTo((screen.width-800)/2,screen.height/2);
	
	//window.open('lai_suat_id.php?name=RATECD' + index);
}

//bao tri
function addRowToTable3(num,TERM,TRMCD,RATE,RATECD)
{
	//alert(hasLoaded);
	//alert('123');
	if (hasLoaded && document.getElementById && document.getElementsByTagName) {
		var textbox = document.getElementById('TERMNO');
		var btn_addrecord = document.getElementById('addrecord');
		if(textbox.value>=20){
			btn_addrecord.disabled = true;
			return;	
		}
	 	//alert('ho tro dom');	
		var tbl = document.getElementById(TABLE_NAME);
	
		var nextRow = tbl.tBodies[0].rows.length;
		
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		// add the row
		
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
		//row.className = 'classy' + (iteration % 2);
	
		// CONFIG: This whole section can be configured
		
		// cell 0 - text
		var cell0 = row.insertCell(0);
		cell0.setAttribute('align','center');
		cell0.style.padding = '.0em 0em';
		var textNode = document.createTextNode(iteration);
		cell0.appendChild(textNode);
		
		// cell 1 - input text
		var cell1 = row.insertCell(1);
		cell1.style.padding = '.0em 0px';
		//cell1.setAttribute('align','center');
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', 'TERM' + iteration);
		txtInp1.setAttribute('size', '20');
		txtInp1.setAttribute('value', TERM); // iteration included for debug purposes
		cell1.appendChild(txtInp1);
		
		var cell2 = row.insertCell(2);
		cell2.style.padding = '.0em 0px';
		//cell2.setAttribute('align','center');
		var sel = document.createElement('select');
		sel.name = 'TRMCD' + iteration;
		//sel.style = 'width:150px';
		//sel.setAttribute('style', 'width:150px;');
		sel.options[0] = new Option('D - Ngay', 'D');
		sel.options[1] = new Option('M - Thang', 'M');
		sel.options[2] = new Option('Y - Nam', 'Y');
		if(TRMCD=='D'){
			sel.options[0].selected = true;
		}else{
			if(TRMCD=='M'){
				sel.options[1].selected = true;
			}
			else{
				if(TRMCD=='Y'){
					sel.options[2].selected = true;
				}
			}
		}
		cell2.appendChild(sel);
		
		var cell3 = row.insertCell(3);
		cell3.style.padding = '.0em 0px';
		//cell3.setAttribute('align','center');
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'text');
		txtInp3.setAttribute('name', 'RATE' + iteration);
		txtInp3.setAttribute('size', '20');
		txtInp3.setAttribute('value', RATE); // iteration included for debug purposes
		txtInp3.onblur = function () {checkDecimal(this,this.value)};
		txtInp3.className = 'textright';
		cell3.appendChild(txtInp3);
		
		var cell4 = row.insertCell(4);
		cell4.style.padding = '.0em 0px';
		//cell4.setAttribute('align','center');
		var txtInp4 = document.createElement('input');
		txtInp4.setAttribute('type', 'text');
		txtInp4.setAttribute('name', 'RATECD' + iteration);
		txtInp4.setAttribute('size', '20');
		txtInp4.setAttribute('id', 'RATECD' + iteration);
		txtInp4.setAttribute('value', RATECD); // iteration included for debug purposes
		cell4.appendChild(txtInp4);
		
		//cell 2 - input button
		var cell5 = row.insertCell(5);
		cell5.style.padding = '0px 5px';
		var btnEl = document.createElement('input');
		btnEl.setAttribute('type', 'button');
		btnEl.setAttribute('value', '    ');
		btnEl.onclick = function () {openwindow(this,iteration)};
		btnEl.setAttribute('id','replacement_binoculars');
		cell5.appendChild(btnEl);
		
		// cell 2 - input button
		var cell6 = row.insertCell(6);
		cell6.style.padding = '.0em 5px';
		var btnE2 = document.createElement('input');
		btnE2.setAttribute('type', 'button');
		btnE2.setAttribute('value', '     ');
		btnE2.setAttribute('id','replacement_delete');
		btnE2.onclick = function () {deleteCurrentRow(this)};
		cell6.appendChild(btnE2);
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(textNode, txtInp1, sel, txtInp3, txtInp4, btnEl, btnE2);
		
		textbox.value = tbl.tBodies[0].rows.length;
	}
}
